﻿using System.Collections.Generic;
using System.Data;
using ExcelLoader;

namespace MDSAdministration
{
    public class SheetDetailLoad : SheetDetails
    {
        public SheetDetailLoad(string sheetName)
            : base(sheetName)
        {
            ResetValidation();
        }

        public List<string> LColumnInvalidFormat = new List<string>();
        public List<string> LColumnInvalidName = new List<string>();
        public List<string> LHasSystemAttributes = new List<string>();

        public bool IsValid
        {
            get
            {
                return
                    !(IsCodeMissing || IsNameMissing  || IsCodeDuplicate || IsDomainInactive || IsDomainMissing || IsColumnInvalidName || IsColumnInvalidFormat ||
                      IsBkColumnInvalid || IsForeignKeyIvalid || IsHasSystemAttribute || IsCodeNotAutoGenerate || IsSheetNameInvalid || IsCodeNameMatchingInvalid);
            }
        }

        public void ResetValidation()
        {
            IsCodeMissing = false;
            IsNameMissing = false;
            IsCodeDuplicate = false;
            IsDomainInactive = false;
            IsDomainMissing = false;
            IsColumnInvalidName = false;
            IsColumnInvalidFormat = false;
            // isSheetInvalid = false;
            IsBkColumnInvalid = false;
            IsForeignKeyIvalid = false;
            IsHasSystemAttribute = false;
            IsCodeNotAutoGenerate = false;
            IsSheetNameInvalid = false;
            IsCodeNameMatchingInvalid = false;
        }
        //Used for advanced loading
        public bool Load { get; set; }
        public DataTable Data;

        public bool IsCodeMissing { get; set; }
        public bool IsNameMissing { get; set; }
        public bool IsCodeDuplicate { get; set; }
        public bool IsDomainInactive { get; set; }
        public bool IsDomainMissing { get; set; }
        public bool IsColumnInvalidName { get; set; }
        public bool IsColumnInvalidFormat { get; set; }
        public bool IsBkColumnInvalid { get; set; }
        public bool IsForeignKeyIvalid { get; set; }
        public bool IsHasSystemAttribute { get; set; }
        public bool IsCodeNotAutoGenerate { get; set; }
        public bool IsSheetNameInvalid { get; set; }
        public bool IsCodeNameMatchingInvalid { get; set; } //TODO match names and codes

        public string Message()
        {
            string message = "";
            if (IsCodeDuplicate)
            {
                message += "Duplcate Code values have been found.";
            }
            if (IsCodeMissing)
            {
                message += "The column Code can not be found. Check case sensitivity.";
            }
            if (IsNameMissing)
            {
                message += "The column Name can not be found. Check case sensitivity.";
            }
            
            if (IsDomainInactive)
            {
                message += "There are inactive domain values in mds.";
            }
            if (IsDomainMissing)
            {
                message += "There are missing domain values in mds.";
            }
            if (IsColumnInvalidName)
            {
               string cols = "";
                foreach (string col in LColumnInvalidName)
                {
                    if (cols.Length > 0)
                    {
                        cols += ", ";
                    }
                    cols += col;
                }
                message += "The excel sheet: " + Name + " Column(s): " + cols + "  does not exist in the entity.";
            }
            if (IsColumnInvalidFormat)
            {
                string cols = "";
                foreach (string col in LColumnInvalidFormat)
                {
                    if (cols.Length > 0)
                    {
                        cols += ", ";
                    }
                    cols += col;
                }
                message += "The excel sheet: " + Name + " Column(s): " + cols + " are not formated as text.";
            }
            //if (isSheetInvalid)
            //{

            //}
            //if (isBKColumnInvalid)
            //{

            //}
            //if (isForeignKeyIvalid)
            //{

            //}

            if (IsHasSystemAttribute)
            {   
                string cols = "";
                foreach (string col in LHasSystemAttributes)
                {
                    if (cols.Length > 0)
                    {
                        cols += ", ";
                    }
                    cols += col;
                }
                message += "The excel sheet: " + Name + " Column(s): " + cols + " are MDS system attributes.";
            }
            if (IsCodeNotAutoGenerate)
            {
                message += "The entity is not setup to create Code values automatically.";
            }
            if (IsSheetNameInvalid)
            {
                message += "There is no entity that matches the sheet name. Check case sensitivity.";
            }
            
            return message;
        }
    }
}